package lapsnake.core;

import java.awt.BorderLayout;
import java.awt.Panel;
import java.util.LinkedList;
import java.util.Queue;

import owg.util.data.KryoInstance;
import owg.util.data.Pair;

import javax.swing.JPanel;

import lapsnake.workspace.Workspace;

import org.python.util.PythonInterpreter;

public class LapCore
{
    public static JPanel panel;
    public static LapCore core;
    public static Workspace workspace;

    /**
     * @param parent The parent container. This object will add itself.
     * @param constraints The constraints with which to add this object to the parent container via the {@link Panel#add(java.awt.Component, Object)} method. 
     * Should be null if no constraints should be used.
     */
    public LapCore(JPanel parent, Object constraints)
    {
	core = this;
	panel = new JPanel();
	panel.setLayout(new BorderLayout());
	Queue<Pair<String, Runnable>> jobs = new LinkedList<>();
	//jobs.add(new Pair<>("Initializing Audio System", () -> {Audio.init(LapSnake.settings.useOpenAL, LapSnake.settings.jsAudioBufferMultiplier, 1, 1, new File(""));}));
	//jobs.add(new Pair<>("Initializing OpenGL", () -> {GLProfile.initSingleton();}));
	jobs.add(new Pair<>("Initializing Kryo", () -> {KryoInstance.init();}));
	jobs.add(new Pair<>("Initializing Jython", () -> {PythonInterpreter.initialize(System.getProperties(), null, null);}));
	
	panel.add(new LoadingPanel(this, jobs), BorderLayout.CENTER);
	parent.add(panel, constraints);
    }

    /**Expensive libraries and resources have been loaded.*/
    protected static void loadingCompleted()
    {
	workspace = new Workspace();
	panel.removeAll();
	panel.invalidate();
	panel.add(workspace, BorderLayout.CENTER);
	panel.validate();
	panel.repaint();
    }
}
